package com.classroom.smart.service;

import com.classroom.smart.mapper.UserMapper;
import com.classroom.smart.pojo.User;
import com.classroom.smart.pojo.UserExample;
import com.classroom.smart.utils.OSSUtils;
import com.classroom.smart.vo.LoginVO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
public class UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    //注册
    public boolean insert(User user){
        int i = userMapper.insert(user);
        return i>0?true:false;
    }

    //登录
    public User login(LoginVO vo){
        //封装查询条件
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andNameEqualTo(vo.getUsername());
        criteria.andPasswordEqualTo(vo.getPassword());
        //执行查询
        List<User> users = userMapper.selectByExample(userExample);
        return users.size() > 0 ? users.get(0) : null;
    }

    //根据条件分页查询用户列表
    public List<User> viewAllByQuery(int current , String query){
        //封装查询条件
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andNameLike("%"+query+"%");

        //PageHelper的分页插件
        PageHelper.startPage(current , 3);

        //执行Mapper接口的查询操作
        List<User> list = userMapper.selectByExample(userExample);
        return list;
    }

    //删除操作
    public boolean delete(int userid) {
        int i = userMapper.deleteByPrimaryKey(userid);
        return i>0?true:false;
    }

    //根据ID查询用户信息
    public User viewOne(int userid) {
        User user = userMapper.selectByPrimaryKey(userid);
        return user;
    }

    //更新用户信息
    public boolean update(User user){
        int i = userMapper.updateByPrimaryKeySelective(user);
        return i>0?true:false;
    }
}
